package com.ymttest.business.service;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ymatou.iapi.marketingmanage.parameter.*;
import com.ymatou.iapi.marketingmanage.service.*;
import com.ymatou.iapi.trading.operate.parameter.enums.PromotionTypeEnum;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymttest.database.mongo.NewSellerPromotionInfoWapper;
import com.ymttest.database.mongo.SellerPromotionInfoWapper;
import com.ymttest.database.mongo.YmtBuyerProductWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;

/************************************************************************************
 * @File name :JSellerPromotionCallService.java
 * @Author : zhouyi
 * @Date : 2017年5月26日
 * @Copyright : 洋码头
 ************************************************************************************/
public class JSellerPromotionCallService {

	/**
	 * 初始化创建满减活动,默认全场活动
	 * 
	 * @param sellerid
	 *            商家id
	 * @param promotionType
	 *            满减类型
	 * @return
	 */
	public static CreateSellerPromotionBean initCreateSellerPromotionBean(int sellerid, PromotionTypeEnum promotionType) {
		CreateSellerPromotionBean cspb = new CreateSellerPromotionBean();
		cspb.setSellerId(sellerid);
		PromotionPolicy PromotionInfo = new PromotionPolicy();
		PromotionInfo.setCanUseSellerCoupon(false);
		PromotionInfo.setIsLimitProduct(false);
		PromotionInfo.setIsSameProductUse(false);
		PromotionInfo.setIsSpecifyProduct(false);
		PromotionInfo.setProductIds(null);
		PromotionInfo.setPromotionBeginTime(YMTDateUtil.getBeforeOrNextDay(0, YMTDateUtil.YMDTHMS_STRING));
		PromotionInfo.setPromotionDesc(sellerid + ":满减描述" + TestDataManager.getRandomUUID().toString());
		PromotionInfo.setPromotionEndTime(YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMS_STRING));
		PromotionInfo.setPromotionName(sellerid + "满减");
		PromotionInfo.setPromotionType(promotionType.getCode());
		List<PromotionValue> PromotionValues = new ArrayList<>();
		PromotionValue e = new PromotionValue();
		e.setAmountCondition(new BigDecimal(5));
		e.setQtyCondition(5);
		e.setValue(new BigDecimal(1));
		if(promotionType.getCode()==5){
//			e.setAmountCondition(new BigDecimal(5));
			e.setAmountCondition(null);
			e.setQtyCondition(2);
			e.setValue(new BigDecimal(0.5));
		}
		PromotionValues.add(e);
		PromotionInfo.setPromotionValues(PromotionValues);
		cspb.setPromotionInfo(PromotionInfo);
		List<String> ProductId = new ArrayList<>();
		PromotionInfo.setProductIds(ProductId);
		return cspb;
	}
	
	public static CreateSellerPromotionBean initCreateSellerPromotionBeanForSpeicalProduct(int sellerid, PromotionTypeEnum promotionType) throws Exception {
		CreateSellerPromotionBean cspb = new CreateSellerPromotionBean();
		cspb.setSellerId(sellerid);
		PromotionPolicy PromotionInfo = new PromotionPolicy();
		PromotionInfo.setCanUseSellerCoupon(false);
		PromotionInfo.setIsLimitProduct(false);
		PromotionInfo.setIsSameProductUse(false);
		PromotionInfo.setIsSpecifyProduct(true);
		PromotionInfo.setProductIds(null);
		PromotionInfo.setPromotionBeginTime(YMTDateUtil.getBeforeOrNextDay(0, YMTDateUtil.YMDTHMS_STRING));
		PromotionInfo.setPromotionDesc(sellerid + ":特定商品满减描述" + TestDataManager.getRandomUUID().toString());
		PromotionInfo.setPromotionEndTime(YMTDateUtil.getBeforeOrNextDay(2, YMTDateUtil.YMDTHMS_STRING));
		PromotionInfo.setPromotionName(sellerid + "满减");
		PromotionInfo.setPromotionType(promotionType.getCode());
		List<PromotionValue> PromotionValues = new ArrayList<>();
		PromotionValue e = new PromotionValue();
		e.setAmountCondition(new BigDecimal(10));
		e.setQtyCondition(2);
		e.setValue(new BigDecimal(1));
		PromotionValues.add(e);
		PromotionInfo.setPromotionValues(PromotionValues);
		
		List<String> productIds = new ArrayList<>();
		
		List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);

		productIds.add(bpw.get(0).get("ProductId").toString());
		productIds.add(bpw.get(1).get("ProductId").toString());
		productIds.add(bpw.get(2).get("ProductId").toString());
		productIds.add(bpw.get(3).get("ProductId").toString());
		
		PromotionInfo.setProductIds(productIds);

		cspb.setPromotionInfo(PromotionInfo);
		return cspb;
	}
	
	public static CreateSellerPromotionBean initCreateSellerPromotionBeanForExcludeProduct(int sellerid, PromotionTypeEnum promotionType) throws Exception {
		CreateSellerPromotionBean cspb = new CreateSellerPromotionBean();
		cspb.setSellerId(sellerid);
		PromotionPolicy PromotionInfo = new PromotionPolicy();
		PromotionInfo.setCanUseSellerCoupon(false);
		PromotionInfo.setIsLimitProduct(true);
		PromotionInfo.setIsSameProductUse(false);
		PromotionInfo.setIsSpecifyProduct(false);
		PromotionInfo.setProductIds(null);
		PromotionInfo.setPromotionBeginTime(YMTDateUtil.getBeforeOrNextDay(0, YMTDateUtil.YMDTHMS_STRING));
		PromotionInfo.setPromotionDesc(sellerid + ":排除商品满减描述" + TestDataManager.getRandomUUID().toString());
		PromotionInfo.setPromotionEndTime(YMTDateUtil.getBeforeOrNextDay(2, YMTDateUtil.YMDTHMS_STRING));
		PromotionInfo.setPromotionName(sellerid + "满减");
		PromotionInfo.setPromotionType(promotionType.getCode());
		List<PromotionValue> PromotionValues = new ArrayList<>();
		PromotionValue e = new PromotionValue();
		e.setAmountCondition(new BigDecimal(10));
		e.setQtyCondition(2);
		e.setValue(new BigDecimal(1));
		PromotionValues.add(e);
		PromotionInfo.setPromotionValues(PromotionValues);
		
		List<String> productIds = new ArrayList<>();
		
		List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);

		productIds.add(bpw.get(0).get("ProductId").toString());
		productIds.add(bpw.get(1).get("ProductId").toString());
		productIds.add(bpw.get(2).get("ProductId").toString());
		productIds.add(bpw.get(3).get("ProductId").toString());
		
		PromotionInfo.setProductIds(productIds);

		cspb.setPromotionInfo(PromotionInfo);
		return cspb;
	}

	/**
	 * 创建满减
	 * 
	 * @param bean
	 * @return 返回PromotionId
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws InterruptedException 
	 */
	public static Integer CreateSellerPromotion(CreateSellerPromotionBean bean) throws ClientProtocolException, IOException, InterruptedException {
		Logger.comment("创建满减活动");
		CreateSellerPromotionCall cb = new CreateSellerPromotionCall();
		cb.setData(bean);
		cb.callService();
		return cb.getJsonObject().getAsJsonObject("Data").get("PromotionId").getAsInt();
	}
	
	/**
	 * 创建满减
	 * 
	 * @param bean
	 * @return 返回PromotionId
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static Integer CreateOldSellerPromotion(CreateSellerPromotionBean bean) throws ClientProtocolException, IOException {
		Logger.comment("创建满减活动");
		CreateSellerPromotionCall cb = new CreateSellerPromotionCall();
		
//		String url = cb.getUrl();
//		url=url.replace("marketingmanage.iapi.ymatou.com", "marketingseller.api.ymatou.com");
//	
//		cb.setUrl(url);
		cb.setData(bean);
		cb.callService();
		return cb.getJsonObject().getAsJsonObject("Data").get("PromotionId").getAsInt();
	}

	/**
	 * 取消满减活动
	 * 
	 * @param sellerid
	 * @param promotionId
	 * @return 是否取消成功
	 * @throws Exception
	 */
	public static Boolean CancelOrEndPromotion(Integer sellerid) throws Exception {
		Logger.comment("取消满减活动");
		Boolean success = false;
		ArrayList<Map> list = new NewSellerPromotionInfoWapper().getEnablePromotion(sellerid);
		Logger.debug(list);
		if (list != null && !list.isEmpty()) {
			for (Map map : list) {
				Integer promotionId = Integer.valueOf(map.get("_id").toString());
				CancelOrEndPromotionBean bean = new CancelOrEndPromotionBean();
				// 2 - 提前结束活动 现在产线上都是2没有1的情况 by王旭东
				bean.setOperatorType(2);
				bean.setPromotionId(promotionId);
				bean.setSellerId(sellerid);
				CancelOrEndPromotionCall cb = new CancelOrEndPromotionCall();
				cb.setData(bean);
				cb.callService();
				success = cb.getJsonObject().getAsJsonObject("Data").get("IsSuccess").getAsBoolean();
				if(!success){
					bean.setOperatorType(1);
					cb.callService();
				}
			}
			
		}
		return success;
	}
	
	/**
	 * 取消满减活动
	 * 
	 * @param sellerid
	 * @param promotionId
	 * @return 是否取消成功
	 * @throws Exception
	 */
	public static Boolean OldCancelOrEndPromotion(Integer sellerid) throws Exception {
		Logger.comment("取消满减活动");
		Boolean success = false;
		ArrayList<Map> list = new NewSellerPromotionInfoWapper().getEnablePromotion(sellerid);
		Logger.debug(list);
		if (list != null && !list.isEmpty()) {
			for (Map map : list) {
				Integer promotionId = Integer.valueOf(map.get("_id").toString());
				CancelOrEndPromotionBean bean = new CancelOrEndPromotionBean();
				// 2 - 提前结束活动 现在产线上都是2没有1的情况 by王旭东
				bean.setOperatorType(2);
				bean.setPromotionId(promotionId);
				bean.setSellerId(sellerid);
				CancelOrEndPromotionCall cb = new CancelOrEndPromotionCall();
				cb.setData(bean);
				
				String url = cb.getUrl();
				url=url.replace("marketingmanage.iapi.ymatou.com", "marketingseller.api.ymatou.com");
			
				cb.setUrl(url);
				cb.callService();
				success = cb.getJsonObject().getAsJsonObject("Data").get("IsSuccess").getAsBoolean();
			}
			
		}
		return success;
	}

	/**
	 * 查询当前sid现在正在生效的pid
	 * @param sellerid
	 * @return
	 * @throws Exception
	 */
	public static List<Integer> SellerPromotionListByState(Integer sellerid) throws Exception {
		List<Integer> plist=new ArrayList<>();
		SellerPromotionListByStateBean sellerpromotionlistbystateBean=new SellerPromotionListByStateBean();
		SellerPromotionListByStateCall sellerpromotionlistbystateCall=new SellerPromotionListByStateCall();
		sellerpromotionlistbystateBean.setIsShowPromotionDetail(true);
		sellerpromotionlistbystateBean.setSellerId(sellerid);
		sellerpromotionlistbystateCall.setData(sellerpromotionlistbystateBean);
		sellerpromotionlistbystateCall.callService();
		JsonObject json = sellerpromotionlistbystateCall.getJsonObject().getAsJsonObject("Data");
		JsonArray ja = json.get("Promotions").getAsJsonArray();
		for (JsonElement jsonElement : ja) {
			JsonObject recjson =jsonElement.getAsJsonObject();
			
			Promotion recp=(Promotion) FormatUtil.GSONToObject(recjson.toString(), Promotion.class);
			plist.add(recp.getPromotionId());
		}
		return plist;
	}
	/**
	 * 取消当前sid现在正在生效的pid,通过SellerPromotionListByState接口查询
	 * @param sellerid
	 * @return
	 * @throws Exception
	 */
	public static Boolean CancelEnablePromotion(Integer sellerid) throws Exception {
		Logger.comment("取消当前满减活动");
		Boolean success = false;
		List<Integer> list = SellerPromotionListByState(sellerid);
		Logger.debug(list);
		if (list != null && !list.isEmpty()) {
			for (Integer  promotionId: list) {
//				Integer promotionId = Integer.valueOf(map.get("_id").toString());
				CancelOrEndPromotionBean bean = new CancelOrEndPromotionBean();
				// 2 - 提前结束活动 现在产线上都是2没有1的情况 by王旭东
				bean.setOperatorType(2);
				bean.setPromotionId(promotionId);
				bean.setSellerId(sellerid);
				CancelOrEndPromotionCall cb = new CancelOrEndPromotionCall();
				cb.setData(bean);
				cb.callService();
				success = cb.getJsonObject().getAsJsonObject("Data").get("IsSuccess").getAsBoolean();
				//旧站点正在进行type2 未开始1新站点不影响
				if(!success){
					bean.setOperatorType(1);
					cb.callService();
				}
			}
			
		}
		return success;
	}

	public static Boolean CancelOrEndPromotion(Integer sellerid, Integer promotionId) throws Exception {
		Logger.comment("取消满减活动");
		Boolean success = false;
		CancelOrEndPromotionBean bean = new CancelOrEndPromotionBean();
		// 2 - 提前结束活动 现在产线上都是2没有1的情况 by王旭东
		bean.setOperatorType(2);
		bean.setPromotionId(promotionId);
		bean.setSellerId(sellerid);
		CancelOrEndPromotionCall cb = new CancelOrEndPromotionCall();
		cb.setData(bean);
		cb.callService();
		success = cb.getJsonObject().getAsJsonObject("Data").get("IsSuccess").getAsBoolean();
		if(!success){
			bean.setOperatorType(1);
			cb.callService();
		}
		return success;
	}
	public static Boolean CancelOrEndPromotion(Integer sellerid, Integer promotionId,Integer type) throws Exception {
		Logger.comment("取消满减活动");
		Boolean success = false;
		CancelOrEndPromotionBean bean = new CancelOrEndPromotionBean();
		// 2 - 提前结束活动 现在产线上都是2没有1的情况 by王旭东
		bean.setOperatorType(type);
		bean.setPromotionId(promotionId);
		bean.setSellerId(sellerid);
		CancelOrEndPromotionCall cb = new CancelOrEndPromotionCall();
		cb.setData(bean);
		cb.callService();
		success = cb.getJsonObject().getAsJsonObject("Data").get("IsSuccess").getAsBoolean();
		return success;
	}
	public static void main(String[] args) throws Exception {
		Logger.debug(SellerPromotionListByState(108));
	}
}
